package com.example.controller;

import com.example.domian.Emp;
import com.example.page.EmpPageQuery;
import com.example.page.PageBean;
import com.example.result.Result;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //修改员工
    @PostMapping("/update")
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success("修改成功");
    }

    //删除员工
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id){
        empService.deleteById(id);
        return Result.success("删除成功");
    }

    //根据id查询员工
    @GetMapping("/selectById")
    public Result selectById(@RequestParam("id") Integer id){
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    //添加员工
    @PostMapping("/insert")
    public Result insert(@RequestBody Emp emp){
        empService.insert(emp);
        return Result.success("添加成功");
    }

    //多条件分页查询员工
    @GetMapping("/findListByCondition")
    public Result findListByCondition(EmpPageQuery empPageQuery){
        PageBean<Emp> empList = empService.findListByCondition(empPageQuery);
        return Result.success(empList);
    }

    //利用pageHelper插件进行分页查询
    @GetMapping("/findPageByCondition")
    public Result findPageByCondition(EmpPageQuery empPageQuery) {
        //调用service方法查询当前分页对象
        PageBean<Emp> pageBean = empService.findPageByCondition(empPageQuery);
        //封装Result对象并返回
        return Result.success(pageBean);
    }

}
